<?php

if(!defined('DIR_VSCORE'))
	define('DIR_VSCORE', DIR_LIB.'VsCore'.DIR_SEP);

if(!defined('DIR_JUNONA'))
	define('DIR_JUNONA', DIR_LIB.'Junona'.DIR_SEP);

require_once(DIR_VSCORE.'db.php');

class JunOperation {

	static $_sqlTable = 'operations';

	private $_id = 0;

	private $_model = NULL;

	private $_modelId = 0;

	private $_opId = '';
	private $_name = '';
	
	private $_minutes = 0;
	private $_price = 0;

	static function updateNames($id, $number, $name) {
		//return;	// Ne Raboti

		$authId = VsAuth::Instance()->id();

		$q = "UPDATE `".self::$_sqlTable."` SET `operation_id`='".$number."', `operation_name`='".$name."', updated_by=".$authId.", updated_at=NOW() WHERE id=".$id;

		$db = VsDatabase::Instance();
		$db->query($q);
	}
	//==========================================================================================================

	static function remove($id) {
		//return;	// Ne Raboti

		$authId = VsAuth::Instance()->id();

		$q = "UPDATE `".self::$_sqlTable."` SET removed=1, updated_by=".$authId.", updated_at=NOW() WHERE id=".$id;

		$db = VsDatabase::Instance();
		$db->query($q);
	}
	//==============================================================================

	static function makeNew() {
		//$item = &self::__construct();	// ERROR
		$item = new JunOperation();

		return $item;
	}
	//==========================================================================

	static function changePrice($id, $data) {
		$q = "UPDATE `".self::$_sqlTable."` SET `minutes`='".$data['minutes']."', `price`=".$data['price']." WHERE `id`=".$id;

		//echo $q; exit;

		$db = VsDatabase::Instance();
		$db->query($q);
	}
	//==========================================================================

	static function updateWorkPrices($id, $price, $minDate, $maxDate) {
		$authId = VsAuth::Instance()->id();

		$q = "UPDATE `operators_work` SET price='".$price."', updated_by=".$authId.", updated_at=NOW() WHERE operations_id=".$id." AND date>='".$minDate."' AND date<='".$maxDate."'";

		//echo $q; exit;

		$db = VsDatabase::Instance();
		$db->query($q);
	}
	//==============================================================================

	static function makeFromForm($form) {
		//$item = &self::__construct();	// ERROR
		$item = new JunOperation();

		//$item->_id = $form['id'];

		$item->_modelId = $form['modid'];

		$item->_opId = $form['number'];
		$item->_name = $form['name'];
		
		//$item->_price = $form['price'];
		$item->_minutes = $form['minutes'];
		$item->_price = $item->priceByMinutes();
		

		return $item;
	}
	//==========================================================================

	static function makeFromDb($arRow) {
		//$item = &self::__construct();	// ERROR
		$item = new JunOperation();

		$item->_id = $arRow['id'];

		$item->_modelId = $arRow['model_id'];

		$item->_opId = $arRow['operation_id'];
		$item->_name = $arRow['operation_name'];
		
		$item->_minutes = $arRow['minutes'];
		$item->_price = $arRow['price'];

		return $item;
	}
	//==========================================================================

	private function __construct() {

	}
	//==========================================================================

	function id() { return $this->_id; }

	function operationId() { return $this->_opId; }
	function number() { return $this->_opId; }
	function name() { return $this->_name; }
	
	function minutes() { return $this->_minutes; }
	function price() { return $this->_price; }
	
	
	function priceByMinutes()
	{
		$settings = JunObjectsFactory::makeSettings();
		$priceByMinutes = $this->minutes() * (1 + ( $settings['minuteBonus'] / 100 )) * $settings['minutePrice'];
		
		return $priceByMinutes;
	}
	//============================================================================

	function save() {
		if($this->_id > 0)
			$this->update();
		else
			$this->insert();
	}
	//=============================================================================

	function insert() {
		$auth = VsAuth::Instance();
		$q = "INSERT INTO `".self::$_sqlTable."`(`model_id`, `operation_id`, `operation_name`, `minutes`, `price`, `added_by`) VALUES('".$this->_modelId."', '".$this->_opId."', '".$this->_name."', '".$this->_minutes."', '".$this->_price."', '".$auth->id()."')";
		
		$db = VsDatabase::Instance();
		$db->query($q);
	}
	//=============================================================================

	function update() {
		if($this->_id<1)
			return;

		$authId = VsAuth::Instance()->id();

		$q = "UPDATE `".self::$_sqlTable."` SET name='".$this->_name."', minutes='".$this->_minutes."', price='".$this->_price."', updated_by=".$authId.", updated_at=NOW() WHERE id=".$this->_id;

		$db = VsDatabase::Instance();
		$db->query($q);
	}
	//==============================================================================

	function delete() {
		if($this->_id<1)
			return;

		$q = "DELETE * FROM `".self::$_sqlTable."` WHERE id=".$this->_id;

		$db = VsDatabase::Instance();
		$db->query($q);
	}
	//==============================================================================

};

?>
